package com.demo.demoproject.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CustomString {
    private String string = null;

    /**
     * 添加一个字符串
     *
     * @return 返回自身
     */
    public CustomString addString() {
        return this;
    }

    /**
     * 添加一个字符串
     *
     * @param string 字符串
     * @return 返回自身
     */
    public CustomString addString(String string) {
        if (this.string == null) {
            return new CustomString(string);
        }
        this.string = this.string + string;
        return this;
    }

    public CustomString addStringWithColon(String string) {
        if (this.string == null) {
            return new CustomString(string);
        }
        this.string = this.string + ":" + string;
        return this;
    }

    /**
     * 添加一个字符串
     *
     * @param string 字符串
     * @param separator 分隔符
     * @return 返回自身
     */
    @NotNull
    @Contract(pure = true)
    public CustomString addString(String string, String separator) {
        if (this.string == null) {
            return new CustomString(string);
        }
        this.string = this.string + separator + string;
        return this;
    }

    /**
     * 获取字符串
     *
     * @return 返回字符串
     */
    public String transferToString() {
        return this.string;
    }

    /**
     * 判断字符是否是字母
     *
     * @param c 字符
         * @return 返回是否是字母
     */
    public static boolean isAlphabet(char c) {
        return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
    }

    /**
     * 判断字符是否是数字
     *
     * @param c 字符
     * @return 返回是否是数字
     */
    public static boolean isNumber(char c) {
        return c >= '0' && c <= '9';
    }

    /**
     * 判断字符串是否是数字
     *
     * @param s 字符串
     * @return 返回是否是数字
     */
    public static boolean isNumber(String s) {
        if (s == null) {
            return false;
        }
        int len = s.length();
        for (int i = 0; i < len; i++) {
            if (!isNumber(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断字符是否是字母或数字
     *
     * @param c 字符
     * @return 返回是否是字母或数字
     */
    public static boolean isAlphabetOrNumber(char c) {
        return isAlphabet(c) || isNumber(c);
    }
}
